import javax.swing.tree.TreeNode;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;

/**
 * 9. 根据一棵树的前序遍历与中序遍历构造二叉树。
 * https://leetcode.cn/problems/construct-binary-tree-from-preorder-and-inorder-traversal/
 */
class Solution {
    static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }
    public int preIndex;
    public TreeNode buildTree(int[] preorder, int[] inorder) {


        TreeNode tree = buildTreeCreate(preorder,inorder,0,preorder.length - 1);
        return tree;
    }

    private TreeNode buildTreeCreate(int[] preorder,int[] inorder,int inbegin,int inend) {
        if(inbegin > inend) {
            return null;
        }
        TreeNode root = new TreeNode(preorder[preIndex]);
        
        int rootIndex = findIndex(preorder,preorder[preIndex],inbegin,inend);
        if(rootIndex == -1) {
            return null;
        }
        preIndex++;
        root.left = buildTreeCreate(preorder ,inorder ,inbegin ,rootIndex - 1);
        root.right = buildTreeCreate(preorder ,inorder ,rootIndex + 1 ,inend);
        return root;
    }
    private int findIndex(int[] preorder,int rootIndex ,int inbegin ,int inend) {
        for(int i = inbegin;i <= inend;i++) {
            if(preorder[i] == rootIndex) {
                return i;
            }
        }
        return -1;
    }

    /**
     * 12. 二叉树前序非递归遍历实现 。
     * https://leetcode.cn/problems/binary-tree-preorder-traversal/
     *
     * @param root
     * @return
     */
    public List<Integer> preorderTraversal(TreeNode root) {
        Stack<TreeNode> stack = new Stack<>();
        List<Integer> list = new LinkedList<>();
        TreeNode cur = root;
        //左边已经遍历完了
        while(cur != null || !stack.isEmpty()) {
            while(cur != null) {
                if(cur == null) {
                    break ;
                }
                stack.push(cur);
                list.add(cur.val);
                cur = cur.left;
            }
            TreeNode tmp = stack.pop();
            cur = tmp.right;
        }
        return list;
    }
    /**
     * 13. 二叉树中序非递归遍历实现。
     *https://leetcode.cn/problems/binary-tree-inorder-traversal/
     */
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> list = new LinkedList<>();
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while(cur != null || !stack.isEmpty()) {
            while(cur != null) {
                if(cur == null) {
                    break;
                }
                stack.push(cur);
                cur = cur.left;
            }
            TreeNode tmp = stack.pop();
            list.add(tmp.val);
            cur = tmp.right;
        }
        return list;
    }

    /**
     * 14. 二叉树后序非递归遍历实现。
     * https://leetcode.cn/problems/binary-tree-postorder-traversal/
     */
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> list = new LinkedList<>();
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;

        TreeNode prve = null;

        while (cur != null || !stack.isEmpty()) {
            while (cur != null) {
                stack.push(cur);
                cur = cur.left;
            }

            TreeNode tmp = stack.peek();
            if (tmp.right == null || prve == tmp.right) {
                list.add(tmp.val);
                stack.pop();
                prve = tmp;
            } else {
                cur = tmp.right;
            }
        }
        return list;
    }
}